const express = require('express')
const router = express.Router()
const { verifyToken } = require('../../middilWare/verifyToken')
const {
  createNotice,
  getNoticeList,
  deleteNotice,
} = require('../../services/noticeService')

// 发布公告
router.post('/', verifyToken, async (req, res) => {
  try {
    const { content } = req.body
    const result = await createNotice(content)
    res.status(200).json({ code: 200, message: '发布成功', data: result })
  } catch (error) {
    res.status(500).json({ code: 500, message: '发布失败' })
  }
})

// 获取公告列表
router.get('/', async (req, res) => {
  try {
    const result = await getNoticeList()
    res.status(200).json({ code: 200, message: '获取成功', data: result })
  } catch (error) {
    res.status(500).json({ code: 500, message: '获取失败' })
  }
})

// 删除公告
router.delete('/', verifyToken, async (req, res) => {
  try {
    const { id } = req.query
    const result = await deleteNotice(id)
    res.status(200).json({ code: 200, message: '删除成功', data: result })
  } catch (error) {
    res.status(500).json({ code: 500, message: '删除失败' })
  }
})

module.exports = router
